package com.tabjin.rfidsocket.controller.v1.sys;

import com.tabjin.rfidsocket.common.utils.TabJsonResult;
import com.tabjin.rfidsocket.exception.ParamException;
import com.tabjin.rfidsocket.pojo.ArchBuilding;
import com.tabjin.rfidsocket.pojo.ArchProject;
import com.tabjin.rfidsocket.service.access.ArchBuildingService;
import com.tabjin.rfidsocket.service.access.ArchCardService;
import com.tabjin.rfidsocket.service.access.ArchProjectService;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

/**
 * @author Tabjin
 * create at 2021/3/16 17:39
 * @program rfid_socket
 * @description
 */
@RestController
@RequestMapping(value = "/t")
public class TransparentController {
    private final ArchProjectService archProjectService;
    private final ArchBuildingService archBuildingService;
    private final ArchCardService archCardService;

    public TransparentController(ArchProjectService archProjectService, ArchBuildingService archBuildingService, ArchCardService archCardService) {
        this.archProjectService = archProjectService;
        this.archBuildingService = archBuildingService;
        this.archCardService = archCardService;
    }


    /**
     * 根据县区获取项目
     *
     * @param region
     * @return
     */
    @RequestMapping(value = "/projects/by/district/{region}", method = RequestMethod.POST)
    @ResponseBody
    public TabJsonResult projectsByDistrict(@PathVariable(value = "region", required = true) String region) {
        Optional<List<ArchProject>> projectsByRegionOptional = archProjectService.getProjectsByRegion(region);
        List<ArchProject> result = projectsByRegionOptional.orElseThrow(() -> new ParamException("无项目"));
        return TabJsonResult.ok(result);
    }

    /**
     * 根据项目获取楼栋信息
     *
     * @param projectId
     * @return
     */
    @RequestMapping(value = "/building/by/project/all", method = RequestMethod.POST)
    @ResponseBody
    public TabJsonResult getBuildingsByProjectIdWithoutPage(@RequestParam(value = "projectId", required = true) Integer projectId) {
        List<ArchBuilding> buildingList = archBuildingService.getBuildingsByProjectId(projectId);
        return TabJsonResult.ok(buildingList);
    }

    @RequestMapping(value = "/hasCardByCardCode", method = RequestMethod.POST)
    public TabJsonResult hasCardByCardCode() {
        boolean b = archCardService.hasCardByCardCode("0100020000020000030000FF");
        return TabJsonResult.ok(b);
    }
}
